﻿using daonaite.core.Attribute;
using daonaite.DAL.system;
using daonaite.Model.system.dictType;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace daonaite.services.system
{
    [AutoLoad]
    public class dicttypeService
    {
        private readonly D_dicttype ttcz;

        public dicttypeService(D_dicttype ttcz)
        {
            this.ttcz = ttcz;
        }

        /// <summary>
        /// 查询字典类型
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<List<sys_dicttype>> select(Dto_dicttype data, RefAsync<int> total)
        {
            return await ttcz.select(data, total);
        }

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<sys_dicttype> get(int id)
        {
            return await ttcz.get(id);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<bool> add(sys_dicttype data, RefAsync<string> errmessage)
        {
            RefAsync<int> total = new RefAsync<int>();
            var result = await ttcz.select(new Dto_dicttype() { biaoshi = data.biaoshi }, total);
            if (result.Count > 0)
            {
                errmessage = "标识已存在";
                return false;
            }
            await ttcz.add(data);
            return true;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<bool> updateAsync(sys_dicttype data, RefAsync<string> errmessage)
        {
            RefAsync<int> total = new RefAsync<int>();
            var result = await ttcz.select(new Dto_dicttype() { biaoshi = data.biaoshi }, total);
            if (result.Count > 0 && result.Any(x => x.id != data.id))
            {
                errmessage = "标识已存在";
                return false;
            }

            await ttcz.updateAsync(data);
            return true;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> deleteAsync(int id)
        {
            return await ttcz.deleteAsync(id);
        }
    }
}
